<!doctype html>
<html>
  <head>
<!--
    <script src="http://mrdoob.github.com/three.js/build/Three.js"></script>
-->
    <script src="Three.js"></script>
    <script src="bitview.js"></script>
    <script src="shp.js"></script>
    <script src="shp_three.js"></script>
    <script>
      function save(buffer, name, mimetype) {
        var bb = new WebKitBlobBuilder();
        bb.append(buffer);
        var b = bb.getBlob();
        var url = webkitURL.createObjectURL(b);
        var a = document.createElement('a');
        a.download = name;
        a.mimetype = mimetype;
        a.href = url;
        a.click();
        webkitURL.revokeObjectURL(url);
      }
    </script>
    <script>
      parsedShp = null;
      SHPParser.load('110m_land.shp', 
        function(res) {
          console.log('ok', res);
          parsedShp = res;
          var loader = new THREE.SHPLoader();
          var m = loader.createModel(parsedShp, true);
          m.scale.set(1.01, 1.01, 1.01);
          console.log('created model', m);
          var compressed = loader.compress(parsedShp);
          //save(compressed, '110m_land.i12d6')
          var n = loader.loadCompressed(compressed, true);
          var r = new THREE.WebGLRenderer();
          r.setSize(1400,1400);
          document.body.appendChild(r.domElement);
          var s = new THREE.Scene();
          s.add(new THREE.Mesh(
            new THREE.SphereGeometry(88,64, 64),
            new THREE.MeshBasicMaterial({color: 0x0000FF, opacity: 0.3, depthWrite:false})
          ));
          s.add(m);
          s.add(n);
          var c = new THREE.PerspectiveCamera(45, 1, 1, 10000);
          c.position.z = 500;
          c.position.y = 200;
          s.add(c);
          var t0 = new Date;
          var animate = function(t) {
            var dt = t-t0;
            t0 = t;
            m.rotation.y += 0.0001 * dt;
            n.rotation.y += 0.0001 * dt;
            c.lookAt(new THREE.Vector3(0,0,0));
            r.render(s, c);
            requestAnimationFrame(animate);
          }
          requestAnimationFrame(animate);
        },
        function(res){ console.log('error', res); }
      );
    </script>
  </head>
  <body>
  </body>

</html>
